using System.Collections.Generic;
using UnityEngine;

public class PointerController : MonoBehaviour
{
	public static PointerController instance;

	public GameObject pointerPanelPrefab;

	public UIWidget borderWidget;

	public float lerpSpeed = 25f;

	public List<PointerPanel> allPointers;

	public UIRoot uiRoot;

	public Camera mainCamera;

	private void Awake()
	{
		instance = this;
		if (uiRoot == null)
		{
			uiRoot = Object.FindObjectOfType<UIRoot>();
		}
		if (mainCamera == null)
		{
			mainCamera = Camera.main;
		}
		borderWidget = GetComponent<UIWidget>();
	}

	public PointerPanel AddPointer(Transform target, float liveTime = 0f, string iconSpriteName = null, bool drawIconOnVisibleTarget = true, string arrowSpriteName = null, bool multiplePointersOnTarget = false, bool drawArrow = true, bool showDistance = true)
	{
		if (target != null)
		{
			if (multiplePointersOnTarget)
			{
				return InstantiaiePointer(target, liveTime, iconSpriteName, drawIconOnVisibleTarget, arrowSpriteName, drawArrow, showDistance);
			}
			if (!IsPointerExist(target))
			{
				return InstantiaiePointer(target, liveTime, iconSpriteName, drawIconOnVisibleTarget, arrowSpriteName, drawArrow, showDistance);
			}
		}
		return null;
	}

	public PointerPanel AddPointer(Vector3 targetVector, float liveTime = 0f, string iconSpriteName = null, bool drawIconOnVisibleTarget = true, string arrowSpriteName = null, bool multiplePointersOnTarget = false)
	{
		if (multiplePointersOnTarget)
		{
			return InstantiaiePointer(targetVector, liveTime, iconSpriteName, drawIconOnVisibleTarget, arrowSpriteName);
		}
		if (!IsPointerExist(targetVector))
		{
			return InstantiaiePointer(targetVector, liveTime, iconSpriteName, drawIconOnVisibleTarget, arrowSpriteName);
		}
		return null;
	}

	public PointerPanel AddPointer(Vector3 target, GameObject floatingObject, string arrowSpriteName)
	{
		if (!IsPointerExist(target))
		{
			return InstantiatePointer(target, floatingObject, arrowSpriteName);
		}
		return null;
	}

	private PointerPanel InstantiatePointer(Vector3 target, GameObject floatingObject, string arrowSpriteName)
	{
		GameObject gameObject = Object.Instantiate(pointerPanelPrefab, Vector3.zero, Quaternion.identity) as GameObject;
		PointerPanel component = gameObject.GetComponent<PointerPanel>();
		gameObject.transform.SetParent(base.transform);
		gameObject.transform.localScale = Vector3.one;
		floatingObject.transform.SetParent(base.transform);
		floatingObject.transform.position = Vector3.zero;
		floatingObject.transform.localScale = Vector3.one;
		component.floatingObject = floatingObject.transform;
		component.pointToVector = true;
		component.targetVector = target;
		component.iconSprite.spriteName = string.Empty;
		component.arrowSprite.spriteName = ((arrowSpriteName != null) ? arrowSpriteName : string.Empty);
		component.liveTime = 0f;
		component.drawIconOnVisibleTarget = true;
		component.showDistance = false;
		allPointers.Add(component);
		return component;
	}

	private PointerPanel InstantiaiePointer(Transform target, float liveTime = 0f, string iconSpriteName = null, bool drawIconOnVisibleTarget = true, string arrowSpriteName = null, bool drawArrow = true, bool showDistance = true)
	{
		GameObject gameObject = Object.Instantiate(pointerPanelPrefab, Vector3.zero, Quaternion.identity) as GameObject;
		PointerPanel component = gameObject.GetComponent<PointerPanel>();
		component.pointToVector = false;
		component.targetVector = Vector3.one;
		gameObject.transform.SetParent(base.transform);
		gameObject.transform.localScale = Vector3.one;
		component.target = target;
		if (iconSpriteName != null)
		{
			component.iconSprite.spriteName = iconSpriteName;
			if (GameController.instance != null && GameController.instance.curTypeGame == TypeGame.Pixelmon)
			{
				component.iconSprite.transform.localScale = Vector3.one * 2f;
			}
		}
		if (arrowSpriteName != null)
		{
			component.arrowSprite.spriteName = arrowSpriteName;
		}
		component.liveTime = liveTime;
		component.drawIconOnVisibleTarget = drawIconOnVisibleTarget;
		component.drawArrow = drawArrow;
		allPointers.Add(component);
		component.showDistance = showDistance;
		return component;
	}

	private PointerPanel InstantiaiePointer(Vector3 targetVector, float liveTime = 0f, string iconSpriteName = null, bool drawIconOnVisibleTarget = true, string arrowSpriteName = null)
	{
		GameObject gameObject = Object.Instantiate(pointerPanelPrefab, Vector3.zero, Quaternion.identity) as GameObject;
		PointerPanel component = gameObject.GetComponent<PointerPanel>();
		component.pointToVector = true;
		gameObject.transform.SetParent(base.transform);
		gameObject.transform.localScale = Vector3.one;
		component.targetVector = targetVector;
		if (iconSpriteName != null)
		{
			component.iconSprite.spriteName = iconSpriteName;
			if (GameController.instance != null && GameController.instance.curTypeGame == TypeGame.Pixelmon)
			{
				component.iconSprite.transform.localScale = Vector3.one * 2f;
			}
		}
		if (arrowSpriteName != null)
		{
			component.arrowSprite.spriteName = arrowSpriteName;
		}
		component.liveTime = liveTime;
		component.drawIconOnVisibleTarget = drawIconOnVisibleTarget;
		allPointers.Add(component);
		return component;
	}

	public PointerPanel FindPointer(Transform target)
	{
		foreach (PointerPanel allPointer in allPointers)
		{
			if (allPointer.target == target)
			{
				return allPointer;
			}
		}
		return null;
	}

	public PointerPanel FindPointer(Vector3 targetVector)
	{
		foreach (PointerPanel allPointer in allPointers)
		{
			if (allPointer.targetVector == targetVector)
			{
				return allPointer;
			}
		}
		return null;
	}

	public bool IsPointerExist(Transform target)
	{
		if (FindPointer(target) != null)
		{
			return true;
		}
		return false;
	}

	public bool IsPointerExist(Vector3 targetVector)
	{
		if (FindPointer(targetVector) != null)
		{
			return true;
		}
		return false;
	}

	public void RemoveAllPointers()
	{
		if (allPointers == null || allPointers.Count <= 0)
		{
			return;
		}
		foreach (PointerPanel allPointer in allPointers)
		{
			if (allPointer != null)
			{
				Object.Destroy(allPointer.gameObject);
			}
		}
		allPointers.Clear();
	}

	public bool RemovePointer(Transform target)
	{
		PointerPanel pointerPanel = FindPointer(target);
		if (pointerPanel != null && target != null)
		{
			allPointers.Remove(pointerPanel);
			pointerPanel.Remove();
			return true;
		}
		return false;
	}

	public bool RemovePointer(Vector3 targetVector)
	{
		PointerPanel pointerPanel = FindPointer(targetVector);
		if (pointerPanel != null)
		{
			pointerPanel.Remove();
			return true;
		}
		return false;
	}

	public bool RemovePointer(PointerPanel pointer)
	{
		if (pointer != null)
		{
			if (allPointers.Contains(pointer))
			{
				allPointers.Remove(pointer);
			}
			pointer.Remove();
			return true;
		}
		return false;
	}
}
